home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8534 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.6 KB  |  240 lines

  1. Path: news0.accent.net!news
  2. From: Daniel Bolduc <eros-ssm@accent.net>
  3. Newsgroups: comp.lang.c
  4. Subject: C preprocessor problem
  5. Date: Sun, 03 Mar 1996 16:35:10 -0800
  6. Organization: EROS Inc.
  7. Message-ID: <313A3ABE.11CD@accent.net>
  8. NNTP-Posting-Host: 205.236.55.80
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=iso-8859-1
  11. Content-Transfer-Encoding: 8bit
  12. X-Mailer: Mozilla 2.0 (Win16; I)
  13.  
  14. In the past, I programmed with the release  3.2.4 of SCO Operating and
  15. Development System.   The C compiler was Microsoft C 6.0.
  16.  
  17. With this compiler, i can use the maximum of the preprocessor without 
  18. problem.
  19. The preprocessor is recursive, he have to resolve expression until the 
  20. code
  21. is reduce to the simple code.  With  "cc" command i never had any error.
  22.  
  23. But now, with the new compiler of SCO OpenServer, it didn't resolve the 
  24. expression recursively.
  25. It translate expression with the appropriate code, but if the code use a
  26. variable, the reference is not resolve and i get a compiler error.
  27.  
  28. I make an example of little source code to show the problem.  The problem 
  29. is coming with the translate command  "?_FVAL( alias, field )".
  30. The expression        "S_FVAL( Order, iNumber )"
  31. generated this        "zaoFldOrder[ iNumberOrder ].u.cpValue"
  32.  
  33. The reference of "iNumberOrder" is known, but the compiler don't
  34. resolve it. But if i write the code correctly like a result of 
  35. preprocess,
  36. the compiler don't give an error.
  37.  
  38. Does it have a setup to "cc" to eliminate this problem ?
  39.  
  40. If i compile the source with the parameters "-E -P" and i dump the output
  41. on other file.c and I compile again the second file, I resolve the 
  42. problem.
  43.  
  44.  
  45. //========BEGIN OF TEST.C================================================
  46. // test.c
  47. // Daniel Bolduc, March 3, 1996
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51.  
  52. #define ON      1
  53. #define OFF     0
  54.  
  55. typedef int    logic;
  56. typedef int    bitmask;
  57. typedef char   string;
  58.  
  59. #define  function
  60. #define  local
  61.  
  62. #define min(a,b)  ( ( (a) < (b) ) ? (a) : (b) )
  63. #define max(a,b)  ( ( (a) > (b) ) ? (a) : (b) )
  64.  
  65. #define  Min   min
  66. #define  Max   max
  67.  
  68. #define  BoucleFor(_i_,_NbElem_)    for( _i_=0; _i_ < _NbElem_ ; _i_++ )
  69.  
  70. //=========================================================================
  71. // These defines produce the following result:
  72. //
  73. //    DATABASE_RECORD( Employee )
  74. //
  75. // preprocessed code is:
  76. //
  77. //    static int ziAreaEmployee = -1; static sFldEmployee [] = {
  78.  
  79. #define DATABASE_RECORD(idAlias) \
  80.    static int  ziArea##idAlias = -1;   \
  81.    static sFld zaoFld##idAlias [] = {
  82.  
  83. //=========================================================================
  84. // These defines produce the following result:
  85. //
  86. //    USE_FIELD( cName, STRING )
  87. //
  88. // preprocessed code is:
  89. //
  90. //    { "cName", STRING },
  91.  
  92. #define USE_FIELD(idFldName,idType) \
  93.    { #idFldName, idType },
  94.  
  95. #define END_OF_RECORD   \
  96.    { 0 } };
  97.  
  98. //=========================================================================
  99. // These defines produce the following result:
  100. //
  101. //    S_FVAL( Employee, cName )
  102. //
  103. // preprocess code is:
  104. //
  105. //      zaoFldEmployee [cNameEmployee] . u . cpValue
  106.  
  107. #define S_FVAL(idAlias,idFld) \
  108.    zaoFld##idAlias[##idFld##idAlias].u.cpValue
  109. #define C_FVAL(idAlias,idFld) \
  110.    zaoFld##idAlias[##idFld##idAlias].u.cValue
  111. #define I_FVAL(idAlias,idFld) \
  112.    zaoFld##idAlias[##idFld##idAlias].u.iValue
  113. #define L_FVAL(idAlias,idFld) \
  114.    zaoFld##idAlias[##idFld##idAlias].u.lValue
  115. #define R_FVAL(idAlias,idFld) \
  116.    zaoFld##idAlias[##idFld##idAlias].u.rValue
  117.  
  118. //=========================================================================
  119. #define  STRING   0
  120. #define  CHAR     1
  121. #define  INTEGER  2
  122. #define  LOGICAL  3
  123. #define  FLOAT    4
  124.  
  125. static string  *sacpType[] = { "STRING", "CHAR", "INTEGER", "LOGICAL", 
  126. "FLOAT" };
  127.  
  128. //=========================================================================
  129. typedef struct
  130. {
  131.    string *cFieldName;
  132.    int    iType;
  133.    union
  134.    {
  135.       string   *cpValue;
  136.       char     cValue;
  137.       int      iValue;
  138.       logic    lValue;
  139.       float    rValue;
  140.    } u;
  141. } sFld;
  142.  
  143. //=========================================================================
  144.  
  145. DATABASE_RECORD( Test )
  146.    USE_FIELD( iNum,     INTEGER  )
  147.    USE_FIELD( cName,    STRING   )
  148.    USE_FIELD( cSex,     CHAR     )
  149.    USE_FIELD( lActive,  LOGICAL  )
  150.    USE_FIELD( rSalary,  FLOAT    )
  151. END_OF_RECORD
  152.  
  153. enum
  154. {
  155.    iNumTest, cNameTest, cSexTest, lActiveTest, rSalaryTest
  156. };
  157.  
  158. //=========================================================================
  159.  
  160. function main()
  161. {
  162.    local int   liX;
  163.    local sFld  *lopFld;
  164.  
  165.    I_FVAL( Test, iNum      )  = 2240;                // line 
  166. 123
  167.    S_FVAL( Test, cName     )  = "Daniel Bolduc";        // line 
  168. 124
  169.    C_FVAL( Test, cSex      )  = 'M';                // line 
  170. 125
  171.    L_FVAL( Test, lActive   )  = ON;                // line 
  172. 127
  173.    R_FVAL( Test, rSalary   )  = 19.75;                // line 
  174. 128
  175.  
  176.    lopFld = zaoFldTest;
  177.    BoucleFor( liX, 5 )
  178.    {
  179.       printf( "%-20s %-20s Value='"
  180.                , lopFld->cFieldName
  181.                , sacpType [lopFld->iType]
  182.             );
  183.       switch( lopFld->iType )
  184.       {
  185.          case STRING:   printf( "%s'", lopFld->u.cpValue ); break;
  186.          case CHAR:     printf( "%c'", lopFld->u.cValue  ); break;
  187.          case INTEGER:  printf( "%d'", lopFld->u.iValue  ); break;
  188.          case LOGICAL:  printf( "%s'", lopFld->u.lValue ? "ON" : "OFF" ); 
  189. break;
  190.          case FLOAT:    printf( "%f'", lopFld->u.rValue  ); break;
  191.       }
  192.       printf( "\n" );
  193.       ++lopFld;
  194.    }
  195. }
  196. //========END OF TEST.C================================================
  197.  
  198. After doing this command, i got these errors:
  199.  
  200. #cc test.c
  201.  
  202. "test.c", line 123: error: invalid token: [iNumTest
  203. "test.c", line 123: error: Syntax error before or at: ]
  204. "test.c", line 124: error: invalid token: [cNameTest
  205. "test.c", line 125: error: invalid token: [cSexTest
  206. "test.c", line 126: error: invalid token: [lActiveTest
  207. "test.c", line 127: error: invalid token: [rSalaryTest
  208.  
  209.  
  210. If i compile the source like this, the program works:
  211.  
  212. #cc -E -P test.c > test2.c
  213.  
  214. "test.c", line 123: error: invalid token: [iNumTest
  215. "test.c", line 123: error: Syntax error before or at: ]
  216. "test.c", line 124: error: invalid token: [cNameTest
  217. "test.c", line 125: error: invalid token: [cSexTest
  218. "test.c", line 126: error: invalid token: [lActiveTest
  219. "test.c", line 127: error: invalid token: [rSalaryTest
  220.  
  221. #cc test2.c
  222. #a.out
  223.  
  224. iNum                 INTEGER              Value='2240'
  225. cName                STRING               Value='Daniel Bolduc'
  226. cSex                 CHAR                 Value='M'
  227. lActive              LOGICAL              Value='ON'
  228. rSalary              FLOAT                Value='19.750000'
  229.  
  230. -- 
  231.   |\/\/\/|  
  232.   |      |  
  233.   |      |      Daniel Bolduc
  234.   | (o)(o)      eros-ssm@accent.net
  235.   C      _)     MontrΘal, QC, CANADA
  236.    | ,___|  
  237.    |   /    
  238.   /____\    
  239.  /      \
  240.